package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.quest.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void deleteById(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Department record) {
        departmentMapper.insert(record);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void update(Department record) {
        departmentMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
      /*  //查询总条数
        int count = departmentMapper.selectForCount(qo);
        //如果没有数据,则不用查询具体数据
        if (count == 0) {
            return new PageResult<Department>(qo.getCurrentPage(), qo.getPageSize(), 0, Collections.emptyList());
        }
        //如果有数据,查询具体数据*/
        List<Department> departments=departmentMapper.selectForList(qo);

      /*  if (departments.size()<=0&&qo.getCurrentPage()>1){
            qo.setCurrentPage(qo.getCurrentPage()-1);
            departments = departmentMapper.selectForList(qo);
        }*/
        //封装入result对象
       /* PageResult<Department> result = new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), count, departments);*/
        return new PageInfo<>(departments);
    }

    @Override
    public List<Department> selectByIds(Long[] ids) {
        List<Department> list = new ArrayList<>();
        for (Long id : ids) {
            Department department = departmentMapper.selectByPrimaryKey(id);
            list.add(department);
        }
        return list;
    }
}
